package com.u17;

import javax.swing.event.HyperlinkEvent;

/**
 * @author u17
 * https://leetcode-cn.com/problems/implement-strstr/
 */
public class Solution {
    public int strStr(String haystack, String needle) {
        if (haystack == null || needle == null
                || haystack.length() == 0 && needle.length() == 0) {
            return 0;
        }

        /**
         * 这里的判断就显得很多余，因为indexOf本身就有判断
         */
        if (haystack.contains(needle)) {
            return haystack.indexOf(needle);
        }

        return -1;
    }

    /**
     * 改进后
     * @param haystack
     * @param needle
     * @return
     */
    public int strStr2(String haystack, String needle) {
        return haystack.indexOf(needle);
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String haystack = "";
        String needle = "";
        System.out.println(solution.strStr2(haystack, needle));
    }
}
